package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.cache.service.IFlightRedisService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.flight.domain.AirTicketDetail;
import cn.wolfcode.luowowo.flight.domain.Airlines;
import cn.wolfcode.luowowo.flight.domain.Airport;
import cn.wolfcode.luowowo.flight.query.AirTicketDetailQuery;
import cn.wolfcode.luowowo.flight.service.IAirTicketDetailService;
import cn.wolfcode.luowowo.flight.service.IAirlinesService;
import cn.wolfcode.luowowo.flight.service.IAirportService;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("flight")
public class FlightController {

    @Reference
    private IFlightRedisService flightRedisService;

    @Reference
    private IAirTicketDetailService airTicketDetailService;
    @Reference
    private IAirportService airportService;
    @Reference
    private IAirlinesService airlinesService;

    @RequestMapping("")
    public String index(Model model) {

        model.addAttribute("hotFlights",flightRedisService.hotFlights());
        model.addAttribute("initialA", flightRedisService.initialA());
        model.addAttribute("initialF", flightRedisService.initialF());
        model.addAttribute("initialK", flightRedisService.initialK());
        model.addAttribute("initialQ", flightRedisService.initialQ());
        model.addAttribute("initialX", flightRedisService.initialX());

        return "flight/index";
    }


    @RequestMapping("/search")
    public String search( Model model,@ModelAttribute("qo") AirTicketDetailQuery qo) {


        model.addAttribute("hotFlights",flightRedisService.hotFlights());
        model.addAttribute("initialA", flightRedisService.initialA());
        model.addAttribute("initialF", flightRedisService.initialF());
        model.addAttribute("initialK", flightRedisService.initialK());
        model.addAttribute("initialQ", flightRedisService.initialQ());
        model.addAttribute("initialX", flightRedisService.initialX());

        //机场
        List<Airport> orgports = airportService.getPortByDest(qo.getOrgCity());
        model.addAttribute("orgports",orgports);
        List<Airport> destports = airportService.getPortByDest(qo.getDstCity());
        model.addAttribute("destports",destports);

        //航空公司
        List<Airlines> listLines = airlinesService.query();
        model.addAttribute("listLines",listLines);

        //列表
        PageInfo pageInfo = airTicketDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "flight/search";
    }

    @RequestMapping("/find")
    public String find(Model model,@ModelAttribute("qo") AirTicketDetailQuery qo) {
        //列表
        PageInfo pageInfo = airTicketDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "flight/searchTpl";
    }

    @RequestMapping("/getGoTripInfo")
    public String getGoTripInfo(Model model, Long id) {

        AirTicketDetail tripInfo = airTicketDetailService.queryById(id);
        model.addAttribute("tripInfo",tripInfo);

        return "flight/goTripTpl";
    }



    @RequestMapping("/orders")
    public String order(Model model,Long depId,Long bacId) {
        if (depId!=null && depId!=-1L){
            model.addAttribute("depInfo",airTicketDetailService.queryById(depId));
        }
        if (bacId!=null && bacId!=-1L){
            model.addAttribute("bacInfo",airTicketDetailService.queryById(bacId));
        }
        return "flight/orders";
    }

    @RequestMapping("/submit")
    @RequireLogin
    @ResponseBody
    public Object submit(){

        return new AjaxResult(true);
    }

}
